<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>限定输入内容</title>
	</head>
	<body>
		<form>
			年度<input type="text" name="year"/><span id="yearError"></span>
			<br/>
			月份<input type="text" name="month"/><span id="monthError"></span>
			<br/>
			<input type="button" value="查询"/>
		</form>
		
		<script>
			var inputs=document.getElementsByTagName('input');
			var yearError=document.getElementById('yearError');
			var monthError=document.getElementById('monthError');
			
			function checkYear(obj){
				var str=obj.value;
				var reg=/^\d{4}$/;
				if(!str.match(reg)){
					yearError.innerHTML="输入错误，年份为四位数"
				}else{
					yearError.innerHTML='';
				}
			}
			
			inputs[0].onblur=function(){
				checkYear(this);
			}
			
			function checkMonth(obj){
				var str=obj.value;
				var reg=/^(0?[1-9]|1[0-2])$/;
				if(!str.match(reg)){
					monthError.innerHTML="输入错误，月份为01-12"
				}else{
					monthError.innerHTML='';
				}
			}
			inputs[1].onblur=function(){
				checkMonth(this);
			}
		</script>
	</body>
</html>
